package com.example.demo.service;

import com.example.demo.entity.Person;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.logging.Logger;

/**
 * @author huchen
 * @description
 * @since 2021/11/26 17:20
 */
@Service
@Slf4j
public class PersonService {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public void doSomething(Integer sex,Integer share){
        log.info("开始处理任务：sex:{}",sex==1?"男":"女");
        String sql="select * from person2 where sex=? and state=0";
        List<Person> personList = jdbcTemplate.query(sql, new Object[]{sex}, new BeanPropertyRowMapper<>(Person.class));
        log.info("查询条数：num:{}",personList.size());
        if (!CollectionUtils.isEmpty(personList)){
            String sql2="update person2 set state=1 where id = ?";
            personList.forEach(person -> {
                log.info("处理任务：share:{},name:{},id:{},age:{}",share,person.getName(),person.getId(),person.getAge());
                jdbcTemplate.update(sql2,new Object[]{person.getId()});
            });
            /*log.info("处理任务：name:{},id:{},age:{}",person.getName(),person.getId(),person.getAge());
            jdbcTemplate.update(sql2,new Object[]{person.getId()});*/

        }
    }

    public void doSomethingElse(List<Person> personList){
        log.info("开始处理任务：personList:{}",personList);
        for (Person person : personList) {
            String sql2="update person2 set state=1 where id = ?";
            log.info("处理任务：name:{},id:{},age:{}",person.getName(),person.getId(),person.getAge());
            jdbcTemplate.update(sql2,new Object[]{person.getId()});
        }
    }

    public List<Person> findPersonBySex(Integer sex, Integer n){
        String sql = "select * from person2 where state = 0 limit 5";
//        List<Person> personList = jdbcTemplate.query(sql, new Object[]{sex,n}, new BeanPropertyRowMapper<>(Person.class));
        List<Person> personList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Person.class));
        log.info("查询条数：num:{}",personList.size());
        return personList;
    }
}

